package com.myres.service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myres.message.actions.MessageService;
import com.myres.model.Message;
import com.myres.util.GZIPTool;

public class getMyMessages extends ServiceServlet {

	/**
	 * Constructor of the object.
	 */
	public getMyMessages() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
    public void doRequest(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
    	int uid=Integer.parseInt(request.getParameter("uid"));
    	int sender=Integer.parseInt(request.getParameter("sender"));
    	MessageService service =new MessageService();
    	List<Message> messages=service.getThreadDetail(uid, sender);
    	
    	StringBuilder builder=new StringBuilder();
    	builder.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
    	builder.append("<response>");
    	builder.append("<messages>");
    	int i=0;
    	for(Message message:messages){
    		i++;
    		if(i>15) break;
    		builder.append("<message>");
    		
    		builder.append("<m_id>");
    		builder.append(message.getId());
    		builder.append("</m_id>");
    		
    		builder.append("<m_content>");
    		builder.append(message.getContent());
    		builder.append("</m_content>");
    		
    		builder.append("<m_sender>");
    		builder.append(message.getSender());
    		builder.append("</m_sender>");
    		
    		builder.append("<m_sender_name>");
    		builder.append("");
    		builder.append("</m_sender_name>");
    		
    		builder.append("<m_send_time>");
    		builder.append(message.getCreateTime());
    		builder.append("</m_send_time>");
    		
    		builder.append("<m_status>");
    		builder.append(message.getStatus());
    		builder.append("</m_status>");
    		
    		builder.append("</message>");
    	}
    	builder.append("</messages>");
    	builder.append("</response>");
    	
    	response.setCharacterEncoding("utf-8");
    	response.setContentType("text/plain");
    	String content=builder.toString();
    	//content=new GZIPTool().compress(content);
    	//log(content);
    	//content=java.net.URLEncoder.encode(content, "utf-8");
    	log(content);
    	response.getWriter().write(content);
    }
    /*
    public void log(String log){
    	System.out.println(log);
    }
    */
    
    
	public void init() throws ServletException {
		// Put your code here
	}

}
